/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
* FileToRDBDialog.java
*
* Created on December 17, 2007, 9:58 AM
 */
package db.gui;

import fs.genomemap.FileSettings;

import javautil.swing.components.ProgressSupport;
import javautil.swing.components.ProgressSupportableListener;

import java.beans.PropertyChangeEvent;

import javax.swing.*;

/**
 *
 * @author  stewari1
 */
public class FileToRDBDialog extends javax.swing.JDialog implements ProgressSupportableListener {

    /** Field description */
    private ProgressSupport progressSupport = null;

    /** Field description */
    private boolean filesOk = false;


    // Variables declaration - do not modify//GEN-BEGIN:variables

    /** Field description */
    private javax.swing.JPanel buttons_JPanel;

    /** Field description */
    private javax.swing.JButton cancel_JButton;

    /** Field description */
    private javax.swing.JButton clear_JButton;

    /** Field description */
    private javax.swing.JButton ok_JButton;

    /** Field description */
    private javautil.swing.components.ProgressPanel progressPanel;

    /** Field description */
    private RDBConnectionView rDBConnectionView;


    // End of variables declaration//GEN-END:variables

    /**
     * Creates new form FileToRDBDialog
     */
    public FileToRDBDialog(java.awt.Frame parent, boolean modal) {

        super(parent, modal);

        initComponents();

        progressSupport = new ProgressSupport(this);


        // register support and listeners
        registerSupport(progressPanel.getProgressSupport());
        progressPanel.registerSupport(progressSupport);
        rDBConnectionView.getProgressSupport().addPropertyChangeListener(progressPanel);


        // clean the progress panel
        progressPanel.cleanAll();


        // remove the default model checkbox from rDBConnectionView
        rDBConnectionView.setVisibleDefaultModelCheckBox(false);
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */


    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {

        progressPanel     = new javautil.swing.components.ProgressPanel();
        buttons_JPanel    = new javax.swing.JPanel();
        ok_JButton        = new javax.swing.JButton();
        clear_JButton     = new javax.swing.JButton();
        cancel_JButton    = new javax.swing.JButton();
        rDBConnectionView = new RDBConnectionView();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Transfer Files to an RDB");
        ok_JButton.setText("OK");
        ok_JButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ok_JButtonActionPerformed(evt);
            }

        });
        clear_JButton.setText("Clear");
        cancel_JButton.setText("Cancel");
        cancel_JButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancel_JButtonActionPerformed(evt);
            }

        });

        javax.swing.GroupLayout buttons_JPanelLayout = new javax.swing.GroupLayout(buttons_JPanel);

        buttons_JPanel.setLayout(buttons_JPanelLayout);
        buttons_JPanelLayout.setHorizontalGroup(
            buttons_JPanelLayout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                buttons_JPanelLayout.createSequentialGroup().addContainerGap(
                    284, Short.MAX_VALUE).addComponent(ok_JButton).addPreferredGap(
                    javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(
                    clear_JButton).addPreferredGap(
                    javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(
                    cancel_JButton)));
        buttons_JPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, cancel_JButton,
                                      clear_JButton, ok_JButton);
        buttons_JPanelLayout.setVerticalGroup(
            buttons_JPanelLayout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                buttons_JPanelLayout.createParallelGroup(
                    javax.swing.GroupLayout.Alignment.BASELINE).addComponent(
                    cancel_JButton).addComponent(clear_JButton).addComponent(ok_JButton)));
        buttons_JPanelLayout.linkSize(javax.swing.SwingConstants.VERTICAL, cancel_JButton,
                                      clear_JButton, ok_JButton);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup().addContainerGap()
                .addComponent(rDBConnectionView, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing
                    .GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addContainerGap())
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout
                                    .createSequentialGroup().addGap(19, 19, 19)
                                    .addComponent(buttons_JPanel, javax.swing.GroupLayout
                                        .DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short
                                        .MAX_VALUE))
                                            .addGroup(javax.swing.GroupLayout.Alignment
                                                .LEADING, layout.createSequentialGroup()
                                                    .addContainerGap()
                                                        .addComponent(progressPanel, javax.swing
                                                            .GroupLayout.PREFERRED_SIZE, 500, javax
                                                                .swing.GroupLayout.PREFERRED_SIZE)))
                                                                    .addContainerGap(javax.swing
                                                                        .GroupLayout
                                                                            .DEFAULT_SIZE, Short
                                                                                .MAX_VALUE)));
        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, progressPanel, rDBConnectionView);
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                layout.createSequentialGroup().addComponent(
                    rDBConnectionView, javax.swing.GroupLayout.PREFERRED_SIZE,
                    javax.swing.GroupLayout.DEFAULT_SIZE,
                    javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(
                        javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(
                        progressPanel, javax.swing.GroupLayout.PREFERRED_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(
                            javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(
                            buttons_JPanel, javax.swing.GroupLayout.PREFERRED_SIZE,
                            javax.swing.GroupLayout.DEFAULT_SIZE,
                            javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap()));
        pack();

    }    // </editor-fold>//GEN-END:initComponents

    public ProgressSupport getProgressSupport() {
        return progressSupport;
    }

    public void registerSupport(ProgressSupport... progressSupport) {

        progressSupport[0].addPropertyChangeListener(ProgressSupport.PROP_DETAILS_ASKED, this);
        progressSupport[0].addPropertyChangeListener(ProgressSupport.PROP_CANCEL, this);
    }

    public void propertyChange(PropertyChangeEvent evt) {

        if (ProgressSupport.PROP_DETAILS_ASKED.equals(evt.getPropertyName())) {


            // this.setBounds(progressPanel.getBounds());
            this.setSize(this.getPreferredSize());
        }

        if (ProgressSupport.PROP_CANCEL.equals(evt.getPropertyName())) {


            // cancel the job
        }
    }

    /**
     * Method description
     *
     * @param evt description
     */
    private void cancel_JButtonActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_cancel_JButtonActionPerformed
        this.dispose();
    }    // GEN-LAST:event_cancel_JButtonActionPerformed

    /**
     * Method description
     *
     * @param evt description
     */
    private void ok_JButtonActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_ok_JButtonActionPerformed

        if (ok_JButton.getText().equals("Finish")) this.dispose();

        SwingWorker<Void, Void> workerThread = new SwingWorker<Void, Void>() {

            public Void doInBackground() {


                // check if all the input files are present
                String errStr = null;

                if (!filesOk) errStr = FileSettings.checkInputCollection();


                // if input files are okay once.. don`t check twice
                if ((errStr == null) &&!filesOk) {

                    progressSupport.setStatusInfo("All input files are ok.");
                    progressSupport.sleep(1);

                    filesOk = true;
                }

                if (errStr != null) {

                    progressSupport.setStatusInfo(
                        "Some input files were not found. See in Details");
                    progressSupport.setLog(errStr);
                    ok_JButton.setText("Finish");

                    return null;
                }


                // show the progress bars and buttons
                progressPanel.openBarsAndButtons();


                // get the connection model. if fails rDBConnectionView will send out appropriate msges
                RDBConnectionModel rdbConnectionModel = rDBConnectionView.getModel();

                if (rdbConnectionModel == null) return null;

                FileToRDBMover fileToRDBMover =
                    new FileToRDBMover(new RDBConnection(rdbConnectionModel));

                fileToRDBMover.addPropertyChangeListener(progressPanel);
                fileToRDBMover.createSchema();
                ok_JButton.setText("Finish");

                return null;
            }
        };

        workerThread.execute();

    }    // GEN-LAST:event_ok_JButtonActionPerformed

    public RDBConnectionView getRDBConnectionView() {
        return rDBConnectionView;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FileToRDBDialog(new javax.swing.JFrame(), true).setVisible(true);
            }

        });
    }
}
